<template>
  <div>
    <el-form :model="formlist" ref="pwdFormRef" :rules='rules' label-width="100px">
      <el-form-item label="菜单名称" prop="name">
        <el-input v-model="formlist.name"></el-input>
      </el-form-item>
     
      <el-form-item label="是否一级菜单" >
        <el-radio-group v-model="formlist.topLevel">
      <el-radio :label='true'>是</el-radio>
      <el-radio :label='false'>否</el-radio>
    </el-radio-group>
  </el-form-item>
   <el-form-item v-if="formlist.topLevel != true" label="上级菜单">
        <el-select v-model="formlist.parentId" >
    <el-option
      v-for="item in options"
      :key="item.value"
      :label="item.label"
      :value="item.value">
    </el-option>
  </el-select>
  </el-form-item>
  <el-form-item label="菜单路径" prop="routerPath">
    <el-input v-model="formlist.routerPath"></el-input>
  </el-form-item>
    <el-form-item label="排序" prop="order">
        <el-input v-model="formlist.order"></el-input>
      </el-form-item>
    </el-form>
     <div slot="footer" class="dialog-footer">
    <el-button size="small" @click="quxiao">取 消</el-button>
    <el-button size="small" type="primary" @click="queding">确 定</el-button>
  </div>
  </div>
</template>

<script>
import{ addhority,authority,copyreader } from '@/api/limits'
export default {
  props:{
     childata:{
      type: Object,
      require: true
    },
     isedit: {
      type: Boolean,
      required: true
    },
  },
data () {
  return {
    formlist:{
      topLevel:true,
      name:'',
      order:'',
      parentId:'',
      routerPath:'',
      show:true
    },
      q:{
      index:1,
      size:100,
      topLevel:true
    },
    options:[],
    rules:{
      name:[
         { required: true, message: '菜单名称不能为空', trigger: 'blur' },
      ],
      routerPath:[
         { required: true, message: '路径不能为空', trigger: 'blur' },
      ],
      order:[
        { required: true, message: '排序不能为空', trigger: 'blur' },
      ]
    }
  }
},
created(){
 this.loafigthel()
},
methods:{
    async loafigthel(){
       const res = await authority(this.q)
        for(let i = 0; i < res.data.data.length; i++){
          this.options.push({value:res.data.data[i].id,label:res.data.data[i].name})
        }
        if(this.isedit == true){
          this.formlist = this.childata
        }
  },
  // 取消
  quxiao(){
    this.$emit('close')
  },
  // 确定
 async queding(){
  this.$refs.pwdFormRef.validate(async (valid) => {
    if(!valid) return
    if(this.isedit == true){
      const rez = await copyreader(this.formlist)
       this.$message.success(rez.msg)
    } else{
      const res = await addhority(this.formlist)
       this.$message.success(res.msg)
    console.log(res);
    }
    this.$emit('close')
    this.$refs.pwdFormRef.resetFields()
  })
  }
}
}
</script>

<style scoped>
.dialog-footer{
 width: 45%;
 margin-left: 60%;
 margin-top: 20px;
}
.el-form{
  margin-right: 32px;
}
</style>